package com.example.menudemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView labelView = (TextView)findViewById(R.id.label);
        //使用registerForContextMenu()函数，将快捷菜单注册到TextView控件上
        registerForContextMenu(labelView);
    }

    //快捷菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0,MENU_00,0,"打印");
        menu.add(0,MENU_01,1,"新建");
        menu.add(0,MENU_02,2,"邮件");
        menu.add(0,MENU_03,3,"设置");
        menu.add(0,MENU_04,4,"订阅");
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        TextView label = (TextView)findViewById(R.id.label);
        switch (item.getItemId()) {
            case MENU_00:
                label.setText("打印，菜单ID：" + item.getItemId());
                return true;
            case MENU_01:
                label.setText("新建，菜单ID：" + item.getItemId());
                return true;
            case MENU_02:
                label.setText("邮件，菜单ID：" + item.getItemId());
                return true;
            case MENU_03:
                label.setText("设置，菜单ID：" + item.getItemId());
                return true;
            case MENU_04:
                label.setText("订阅，菜单ID：" + item.getItemId());
                return true;
            default:
                return false;
        }
    }

    //选项菜单
    final static int MENU_00 = Menu.FIRST;
    final static int MENU_01 = Menu.FIRST+1;
    final static int MENU_02 = Menu.FIRST+2;
    final static int MENU_03 = Menu.FIRST+3;
    final static int MENU_04 = Menu.FIRST+4;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,MENU_00,0,"打印");
        menu.add(0,MENU_01,1,"新建");
        menu.add(0,MENU_02,2,"邮件");
        menu.add(0,MENU_03,3,"设置");
        menu.add(0,MENU_04,4,"订阅");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        TextView label = (TextView)findViewById(R.id.label);
        switch (item.getItemId()) {
            case MENU_00:
                label.setText("打印，菜单ID：" + item.getItemId());
                return true;
            case MENU_01:
                label.setText("新建，菜单ID：" + item.getItemId());
                return true;
            case MENU_02:
                label.setText("邮件，菜单ID：" + item.getItemId());
                return true;
            case MENU_03:
                label.setText("设置，菜单ID：" + item.getItemId());
                return true;
            case MENU_04:
                label.setText("订阅，菜单ID：" + item.getItemId());
                return true;
            default:
                return false;
        }
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        MenuInflater inflater = getMenuInflater();
//        inflater.inflate(R.menu.main_menu, menu);
//        return true;
//    }
}